package com.leapp.sk.viewdelegate.adapter.classs

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.mvp.delegate.BaseAdapterDelegate
import com.example.mvp.view.GlideCircleTransform
import com.example.mvp.view.LKCircleImageView
import com.leapp.sk.R
import com.leapp.sk.bean.TransferTecherEntity
import com.example.inter.ItemClickInterface
import com.leapp.sk.ui.adapter.TransferTecherAdapter

/**
 * 转让老师适配器视图托管
 * @author  chaohaipeng  on 2018/8/1.
 */
class TransferThcherAdapterDelegate : BaseAdapterDelegate() {
    /**
     * 头像加载操作
     */
    var mHeadRequestOptions: RequestOptions = RequestOptions()
    override fun getOptionsMenuId(): Int {
        return 0
    }

    override fun initWidget() {
    }

    override fun onDestroy() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.adater_transfer_thecher
    }

    /**
     * 初始化数据
     */
    fun initData(context: Context,data: TransferTecherEntity){
        val ima_att_avatar = getView<LKCircleImageView>(R.id.ima_att_avatar)
        val tv_att_name = getView<TextView>(R.id.tv_att_name)
        val iv_att_chose = getView<ImageView>(R.id.iv_att_chose)
        setHead(context, data?.avatar, ima_att_avatar)
        iv_att_chose.isSelected = data.isCheck
        tv_att_name.text=data.name
    }

    /**
     * 初始化点击事件
     */
   fun  initEvent(list:List<TransferTecherEntity>, adapter: TransferTecherAdapter?, position:Int,itemClickInterfaces: ItemClickInterface){
       val iv_att_chose = getView<ImageView>(R.id.iv_att_chose)
       iv_att_chose.setOnClickListener(View.OnClickListener {
           for(i in list.indices){
               val get = list.get(i)
               if (i==position){
                   itemClickInterfaces.itemClick(position)
                   get.isCheck=true
               }else{
                   get.isCheck=false
               }
           }
           adapter?.notifyDataSetChanged()
       })
    }

    /**
     * 设置头像和昵称
     */
    private fun setHead(context: Context, headUrl: String?, headImage: LKCircleImageView) {
        val tag = headImage.getTag(R.id.tag_head)
        if (tag == null || !tag?.equals(headUrl)) {
            val mRequestManager = Glide.with(context)
            mHeadRequestOptions?.diskCacheStrategy(DiskCacheStrategy.NONE)
            mHeadRequestOptions?.skipMemoryCache(true)
            mHeadRequestOptions?.error(R.mipmap.icon_defout_head)
            mHeadRequestOptions?.transforms(GlideCircleTransform(context))
            mRequestManager?.load(headUrl)
                    ?.thumbnail(0.2f)
                    ?.apply(mHeadRequestOptions)
                    ?.into(headImage)
            headImage.setTag(R.id.tag_head, headUrl)
        }
    }
}